package ClientCommTests;

import static org.junit.Assert.*;

import javax.swing.text.html.parser.TagElement;

import org.junit.Test;

import communication.TelnetClient;
import clientCommunicationHandler.ClientServerMessageHandler;
import clientCommunicationHandler.UserInfo;
import clientCommunicationHandler.UserInterfaceMsgHandler;

public class ClientServerMsgHandlerTest {
	
	private final String serverDelimiter = (char)1+"";
	@Test
	public void parseServerMsgLoginSuccessChangeUserNametest() throws Throwable {
		ClientServerMessageHandler tMsgHandler = new ClientServerMessageHandler();
		TelnetClient client = new TelnetClient("localhost", 61613);
		UserInfo.tempUserName = "tamir";
		tMsgHandler.parseMessage("login"+serverDelimiter+"tamir"+serverDelimiter+"success"+serverDelimiter);
		assertEquals("tamir", UserInfo.userName);
	}

//	@Test
//	public void parseServerMsgLoginSuccessChangeMenu() {
//		ClientServerMessageHandler tMsgHandler = new ClientServerMessageHandler();
//		UserInfo.tempUserName = "tamir";
//		tMsgHandler.parseMessage("login"+serverDelimiter+"success"+serverDelimiter);
//		String menu = "[user: tamir] please make your choice:\n\t1. Logout\n\t2. view_Forums\n\t3. Exit";
//		assertEquals(menu, TelnetClient.menu.toString());
//	
//	}
//	
//	@Test
//	public void getRestOfMenuTest(){
//		System.out.println("enter rest of menu");
//		ClientServerMsgHandler tClient = new ClientServerMsgHandler();
//		tClient.getRestOfTheMenu(2);//3 for user that is not log in
//		System.out.println(TelnetClient.menu);
//		assertEquals(TelnetClient.menu, "[user: tamir] please make your choice:\n\t1. Logout\n\t2. view_Forums\n\t3. Exit\n");
//	}
//	
//	@Test 
//	public void threadListaAdminMode() throws Throwable{
//		ClientServerMessageHandler tMsgHandler = new ClientServerMessageHandler();
//		String demoServerMsg = "threadsList"+serverDelimiter+"admin"+serverDelimiter+"1:temp1"+serverDelimiter;
//		UserInfo.userName = "admin";
//		tMsgHandler.parseMessage(demoServerMsg);
//		String realMenu = TelnetClient.menu.toString();
//		String expectedMenu = "[user: admin] please make your choice:" +
//				"\n\t1. Logout" +
//				"\n\t2. viewThreads 1:temp1" +
//				"\n\t3. add-moderator to subForum 1" +
//				"\n\t4. remove-moderator of subForum 1" +
//				"\n\t5. DeleteSubForum 1" +
//				"\n\t6. add-new-SubForum" +
//				"\n\t7. moderatorList";
//		assertEquals(expectedMenu,realMenu);
//		
//	}
//	
	@Test 
	public void threadListaModeratorMode(){
		ClientServerMessageHandler tMsgHandler = new ClientServerMessageHandler();
		String demoServerMsg = "threadsList"+serverDelimiter+"moderator"+serverDelimiter+"1:temp1"+serverDelimiter;
		UserInfo.userName = "moderator";
		tMsgHandler.parseMessage(demoServerMsg);
		String expectedMenu = "[user: moderator] please make your choice:\n\t1. Logout" +
				"\n\t2. viewThreads 1:temp1"+
				"\n\t3. add" +
				"\n\t4. back\n";
		String realMenu = TelnetClient.menu.toString();
		assertEquals(expectedMenu,realMenu);
		
	}
	@Test 
	public void threadListAdminMode(){
		ClientServerMessageHandler tMsgHandler = new ClientServerMessageHandler();
		String demoServerMsg = "threadsList"+serverDelimiter+"admin"+serverDelimiter+"1:temp1"+serverDelimiter;
		UserInfo.userName = "moderator";
		tMsgHandler.parseMessage(demoServerMsg);
		String expectedMenu = "[user: moderator] please make your choice:\n\t1. Logout" +
				"\n\t2. viewThreads 1:temp1"+
				"\n\t3. add" +
				"\n\t4. back\n";
		String realMenu = TelnetClient.menu.toString();
		assertEquals(expectedMenu,realMenu);
		
	}
	
	@Test 
	public void threadListamemberMode(){
		ClientServerMessageHandler tMsgHandler = new ClientServerMessageHandler();
		String demoServerMsg = "threadsList"+serverDelimiter+"member"+serverDelimiter+"1:temp1"+serverDelimiter;
		UserInfo.userName = "m";
		tMsgHandler.parseMessage(demoServerMsg);
		String expectedMenu = "[user: m] please make your choice:\n\t1. Logout" +
				"\n\t2. viewThreads 1:temp1"+
				"\n\t3. add" +
				"\n\t4. back\n";
		String realMenu = TelnetClient.menu.toString();
		assertEquals(expectedMenu,realMenu);
		
	}
	@Test 
	public void threadListaGuestMode(){
		ClientServerMessageHandler tMsgHandler = new ClientServerMessageHandler();
		String demoServerMsg = "threadsList"+serverDelimiter+"guest"+serverDelimiter+"1:temp1"+serverDelimiter;
		UserInfo.userName = "guest";
		tMsgHandler.parseMessage(demoServerMsg);
		String expectedMenu = "[user: guest] please make your choice:" +
				"\n\t1. Login" +
				"\n\t2. Register" +
				"\n\t3. viewThreads 1:temp1"+
				"\n\t4. back\n";
		String realMenu = TelnetClient.menu.toString();
		assertEquals(expectedMenu,realMenu);
		
	}
	
	@Test 
	public void addNewSubForum(){
		ClientServerMessageHandler tMsgHandler = new ClientServerMessageHandler();
		String demoServerMsg = "addedSubForum"+serverDelimiter+"admin"+serverDelimiter+"success"+serverDelimiter+"1"+serverDelimiter+"test"+serverDelimiter+"Tests"+serverDelimiter+"test"+serverDelimiter;
		UserInfo.userName = "test";
		tMsgHandler.parseMessage(demoServerMsg);
		String expectedMenu = "[user: test] choose new moderator for sub forum 1:" +
				"\n\t1. Logout"+
				"\n\t2. Tests" +
				"\n\t3. test"+
				"\n\t4. back";
		String realMenu = TelnetClient.menu.toString();
		assertEquals(expectedMenu,realMenu);
		
	}
	@Test 
	public void addNewmoderatorToexistForum(){
		ClientServerMessageHandler tMsgHandler = new ClientServerMessageHandler();
		String demoServerMsg = "candidatesList"+serverDelimiter+"test"+serverDelimiter+"Tests"+serverDelimiter+"test"+serverDelimiter;
		UserInfo.userName = "test";
		tMsgHandler.parseMessage(demoServerMsg);
		String expectedMenu = "[user: test] choose new moderator for sub forum 1:" +
				"\n\t1. Logout"+
				"\n\t2. test" +
				"\n\t3. Tests"+
				"\n\t4. test"+
				"\n\t5. back";
		String realMenu = TelnetClient.menu.toString();
		assertEquals(expectedMenu,realMenu);
		
	}
	@Test 
	public void getModeratorsList(){
		ClientServerMessageHandler tMsgHandler = new ClientServerMessageHandler();
		String demoServerMsg = "moderatorsList"+serverDelimiter+"test"+serverDelimiter+"Tests"+serverDelimiter+"test"+serverDelimiter;
		UserInfo.userName = "test";
		UserInfo.wantToRemoveModeratorToSubForum ="1";
		tMsgHandler.parseMessage(demoServerMsg);
		String expectedMenu = "[user: test] moderators of subForum: 1:" +
				"\n\t1. Logout"+
				"\n\t2. remove test" +
				"\n\t3. remove Tests"+
				"\n\t4. remove test"+
				"\n\t5. back";
		String realMenu = TelnetClient.menu.toString();
		assertEquals(expectedMenu,realMenu);
		
	}
	
	public void testGetModeratorsList(){
		ClientServerMessageHandler tMsgHandler = new ClientServerMessageHandler();
		UserInfo.userName = "admin";
		UserInfo.wantToAddModeratorToSubForum  = "1";
		String demoServerMsg = "ModeratorsList\nUserRoll\nu1\nu2\n";
		tMsgHandler.parseMessage(demoServerMsg);
		String expected = "[user: admin] moderators of subForum: 1:\n\t1. Logout\n\t2. u1\n\t3. u2\n\t4. back";
		String realMenu = TelnetClient.menu.toString();
		assertEquals(expected, realMenu);
	}

	
	@Test
	public void parseServerMsgPostList(){
		ClientServerMessageHandler tMsgHandler = new ClientServerMessageHandler();
		String delimiter1 =(char)1+"";
		String delimiter2 =(char)2+"";
		String messageFromServer = "postList"+delimiter1+"member"+delimiter1+"1:p1"+delimiter2+
				"owner"+delimiter2+"this is body" +
				delimiter1+
				"2:p2"+delimiter2+"member"+delimiter2+"this is body2"+delimiter1;
		UserInfo.userName = "g";
		tMsgHandler.parseMessage(messageFromServer);
		String realMenu = TelnetClient.menu.toString();
		String expectedMenu = "[user: g] please make your choice:" +
				"\n\t1. Logout" +
				"\n\t2. post 1:p1 content: this is body" +
				"\n\t3. removePost 1:p1" +
				"\n\t4. editPost 1:p1" +
				"\n\t5. post 2:p2 content: this is body2" +
				"\n\t6. addPost 1" +
				"\n\t7. addPost 2" +
				"\n\t8. back\n";
		assertEquals(expectedMenu, realMenu);
//		assertEquals(newMenu,"\n\t1. threadList 3:temp1\n\t2. add\n\t3. back\n");
		
		
	}
	
	@Test
	public void parseServerMsgPostListWithNewLine(){
		ClientServerMessageHandler tMsgHandler = new ClientServerMessageHandler();
		String delimiter1 =(char)1+"";
		String delimiter2 =(char)2+"";
		String messageFromServer = "postList"+delimiter1+"member"+delimiter1+"1:p1"+delimiter2+
				"owner"+delimiter2+"this is\n body" +
				delimiter1+
				"2:p2"+delimiter2+"member"+delimiter2+"this is body2"+delimiter1;
		UserInfo.userName = "g";
		tMsgHandler.parseMessage(messageFromServer);
		String realMenu = TelnetClient.menu.toString();
		String expectedMenu = "[user: g] please make your choice:" +
				"\n\t1. Logout" +
				"\n\t2. post 1:p1 content: this is\n body" +
				"\n\t3. removePost 1:p1" +
				"\n\t4. editPost 1:p1" +
				"\n\t5. post 2:p2 content: this is body2" +
				"\n\t6. addPost 1" +
				"\n\t7. addPost 2" +
				"\n\t8. back\n";
		assertEquals(expectedMenu, realMenu);
//		assertEquals(newMenu,"\n\t1. threadList 3:temp1\n\t2. add\n\t3. back\n");
		
		
	}
	@Test
	public void parseServerMsgPosOwnerOfTwotList(){
		ClientServerMessageHandler tMsgHandler = new ClientServerMessageHandler();
		String delimiter1 =(char)1+"";
		String delimiter2 =(char)2+"";
		String messageFromServer = "postList"+delimiter1+"admin"+delimiter1+
				"1:p1"+delimiter2+"owner"+delimiter2+"this is body" +
				delimiter1+
				"2:p2"+delimiter2+"admin"+delimiter2+"this is body2"+delimiter1;;
		UserInfo.userName = "g";
		tMsgHandler.parseMessage(messageFromServer);
		String realMenu = TelnetClient.menu.toString();
		String expectedMenu = "[user: g] please make your choice:" +
				"\n\t1. Logout" +
				"\n\t2. post 1:p1 content: this is body" +
				"\n\t3. removePost 1:p1" +
				"\n\t4. editPost 1:p1" +
				"\n\t5. post 2:p2 content: this is body2" +
				"\n\t6. removePost 2:p2" +
				"\n\t7. editPost 2:p2" +
				"\n\t8. addPost 1" +
				"\n\t9. addPost 2" +
				"\n\t10. back\n";
		assertEquals(expectedMenu, realMenu);
//		assertEquals(newMenu,"\n\t1. threadList 3:temp1\n\t2. add\n\t3. back\n");
		
		
	}
//	
//	

}
